package com.zk.algorithm;

public class ExcelNumberToColumnTitle {

    public String toColumnTitle(int number) {
        if (number < 1) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        while (number > 0) {
            int reminder = number % 26;

            if (reminder == 0) {
                sb.append('Z');
                number = number / 26 - 1;
            } else {
                // 注意这个地方的强制类型转换
                sb.append((char)((reminder - 1) + 'A'));
                number = number / 26;
            }
        }

        return sb.reverse().toString();
    }

    public static void main(String...args) {
        System.out.println(new ExcelNumberToColumnTitle().toColumnTitle(1));
        System.out.println(new ExcelNumberToColumnTitle().toColumnTitle(2801));
        System.out.println(new ExcelNumberToColumnTitle().toColumnTitle(2782));
    }

}
